package com.delicaterock.mpw;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.SimpleCursorAdapter;
import android.widget.AdapterView.OnItemClickListener;

public class WalletScreen extends ListActivity implements OnItemClickListener {

    private SQLiteDatabase db;

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        SimpleDBHelper dbHelper = new SimpleDBHelper(this);
        db = dbHelper.getReadableDatabase();
        Cursor c = dbHelper.getWalletCursor(db);
        startManagingCursor(c);
        
        // Map Cursor columns to views defined in simple_list_item_2.xml
        ListAdapter adapter = new SimpleCursorAdapter(this,
                android.R.layout.simple_list_item_1, c, 
                        new String[] { SimpleDBHelper.FNAME }, 
                        new int[] { android.R.id.text1 });
        setListAdapter(adapter);
        getListView().setOnItemClickListener(this);
    }
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		db.close();
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        Intent it = new Intent(WalletScreen.this, ConnectionScreen.class);
		it.putExtra("id", String.valueOf(arg3));
		startActivity(it);
	}
}